package com.jxaummd.aquaculture.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jxaummd.aquaculture.R;
import com.jxaummd.aquaculture.entity.Funcation;
import com.jxaummd.aquaculture.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;


public class FuncationAdapter extends RecyclerView.Adapter<FuncationAdapter.VH> {
    private LayoutInflater mInflater;
    private List<Funcation> mItems = new ArrayList<>();

    private OnItemClickListener mClickListener;
    private Context context ;

    public FuncationAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.context = context;
    }

    public void setDatas(List<Funcation> beans) {
        mItems.clear();
        mItems.addAll(beans);
        notifyDataSetChanged();
    }

    public void refreshMsg(Funcation bean) {
        int index = mItems.indexOf(bean);
        if (index < 0) return;
        notifyItemChanged(index);
    }

    @Override
    public VH onCreateViewHolder(final ViewGroup parent, int viewType) {
        final View view = mInflater.inflate(R.layout.item_device_funcation, parent, false);
        final VH holder = new VH(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClickListener != null) {
                    mClickListener.onItemClick(holder.getAdapterPosition(), v, holder);
                }
            }
        });
        holder.imgAvatar.setColorFilter(context.getResources().getColor(R.color.greay));
        return holder;
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        Funcation item = mItems.get(position);
        holder.tvName.setText(item.getName());
        holder.imgAvatar.setImageResource(item.getImageId());
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mClickListener = listener;
    }



    class VH extends RecyclerView.ViewHolder {
        private ImageView imgAvatar;
        private TextView tvName;

        public VH(View itemView) {
            super(itemView);
            imgAvatar = itemView.findViewById(R.id.item_device_fun_image);
            tvName =  itemView.findViewById(R.id.item_device_fun_name);

        }
    }
}
